define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'shop/shopdevice/index' + location.search,
                    add_url: 'shop/shopdevice/add',
                    edit_url: 'shop/shopdevice/edit',
                    del_url: 'shop/shopdevice/del',
                    multi_url: 'shop/shopdevice/multi',
                    import_url: 'shop/shopdevice/import',
                    table: 'shop_device',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                singleSelect: true,
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id')},
                        {field: 'device_sn', title: __('设备编号'), operate: 'LIKE'},
                        {
                            field: 'status',
                            title: __('是否在线'),
                            searchList: {"offline": __('离线'), "online": __('在线')},
                            formatter: Table.api.formatter.status
                        },

                        {field: 'shop.name', title: __('店铺名字')},
                        {
                            field: 'col_mergre',
                            title: __('包厢/台桌名'),
                            operate: false,
                            formatter: function (value, row, index) {
                                var deviceType = row.device_type;
                                if (deviceType === 'qp') {
                                    // return '包厢';
                                    var roomVal = JSON.stringify(row.room);
                                    var newRoom = JSON.parse(roomVal);
                                    return newRoom.name;
                                } else {
                                    var tableVal = JSON.stringify(row.table);
                                    var newTable = JSON.parse(tableVal);
                                    return newTable.name;
                                }
                            }
                        },

                        {
                            field: 'device_type',
                            title: __('设备类型'),
                            searchList: {"qp": __('棋牌'), "tq": __('台球')},
                            formatter: function (value) {
                                if (value === 'qp') {
                                    return '棋牌';
                                } else {
                                    return '台球';
                                }
                            }
                        },
                        {
                            field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate,

                            buttons: [
                                {
                                    name: 'ajax',
                                    title: __('状态查询'),
                                    text: "状态查询",
                                    classname: 'btn btn-xs btn-success btn-magic btn-ajax',
                                    url: function (row) {
                                        return "shop/shopdevice/queryStatus?device_sn=" + row['device_sn'];
                                    },
                                    success: function (data, ret) {

                                        Layer.alert(ret.msg);

                                    },
                                    error: function (data, ret) {
                                        console.log(data, ret);
                                        Layer.alert(ret.msg);
                                        return false;
                                    }
                                },
                            ],
                            formatter: function (value, row, index) {
                                var that = $.extend({}, this);
                                var table = $(that.table).clone(true);
                                $(table).data("operate-del", null);

                                that.table = table;
                                return Table.api.formatter.operate.call(that, value, row, index);
                            }
                        }
                    ]
                ]
            });


            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            //同步包厢
            $("#c-room_id").data("params", function () {
                return {custom: {shop_id: $("#c-shop_id").val()}};
            });

            //同步台桌
            $("#c-table_id").data("params", function () {
                return {custom: {shop_id: $("#c-shop_id").val()}};
            });

            var newVal = $('#c-device_type').val();
            if (newVal === 'qp') {
                $('#c-device_type').val('棋牌');
                $("#c-table_id").val('');
                $('#room').show();
                $('#table').hide();
            }
            if (newVal === 'tq') {
                $('#c-device_type').val('台球');
                $("#c-room_id").val('');
                $('#room').hide();
                $('#table').show();
            }

            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});
